#include "stdafx.h"
#include "player.h"
#include "baloon.h"

using namespace Ogre;




Player::Player(Ogre::SceneManager * sceneMgr,NxOgre::World* m_World,NxOgre::Scene* m_Scene,Critter::RenderSystem*  m_RenderSystem,irrklang::ISoundEngine* sEngine)
{
	tslf=0;
	cameraWalkFinisher=0;
	slowingDown=1;
	Gbody=NULL;
	climbCam=0;
	soundEngine=sEngine;
	walkSoundTimer=0;
	climbTimer=0;
	fallPitch=0;
	groundID=-1;
	zrychlenie=0.5;
	movespeed=17;
	crouch=0;
	crouch_am=0;
	jumpPitch=0;
	cam_walking=0;
	gNormal=Vector3::ZERO;
	gNormal.y=1;
	veee=0;
	smer=Vector3::ZERO;
	mSceneMgr=sceneMgr;
	mWorld=m_World;
	mScene=m_Scene;
	mRenderSystem=m_RenderSystem;
	vpravo=false;
	grabbed=false;
	canGrab=false;
	vlavo=false;
	vpred=false;
	vzad=false;
	stoji=true;
	moze_liezt=false;
	visi=false;
	leti=false;
	nazemi=false;
	camAngle=0;
	lastSpeed=0;
	mSceneMgr->getCamera("Camera")->setPosition(0,0,0);
	mSceneMgr->getCamera("Camera")->lookAt(Vector3(0,0,-1));
	Ogre::Entity* ent = mSceneMgr->createEntity( "name", "play2.mesh" );
	SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode( "CenterNode" );
	node->attachObject( ent );
	//node->setScale(1,2,1);
    ent->setCastShadows( false );
	ent->setVisible(false);
	//ent->setMaterialName( "PSSM/Knot");

	  Critter::BodyDescription bodyDescription;
	  bodyDescription.mMass = 10; 
	  bodyDescription.mLinearDamping=2;
	
       pbody = mRenderSystem->createBody(NxOgre::CapsuleDescription(0.6,2), NxOgre::Vec3(10,10,-10), bodyDescription);
	  pbody->setSceneNode(node);
	  pbody->raiseDynamicFlag(NxOgre::DynamicRigidbodyFlags::Flags::FreezeRotation);

/*
	OgreNewt::ConvexCollisionPtr col = OgreNewt::ConvexCollisionPtr(new OgreNewt::CollisionPrimitives::ConvexHull(m_World,ent,1));
	pbody = new OgreNewt::Body( m_World, col );
	pbody->setContinuousCollisionMode(1); 
	OgreNewt::UpVector* uv= new OgreNewt::UpVector(pbody, Vector3::UNIT_Y);
	OgreNewt::UpVector* uv2= new OgreNewt::UpVector(pbody, Vector3::UNIT_Z);

	Ogre::Vector3 inertia, offset;
    col->calculateInertialMatrix(inertia, offset);
#ifdef OGRENEWT_NO_COLLISION_SHAREDPTR
			//no longer need the collision shape object
			delete col;
#endif
			pbody->setMassMatrix( 0.8, inertia );
            pbody->setCenterOfMass(offset);
			pbody->setContinuousCollisionMode(1);
	pbody->setPositionOrientation( Ogre::Vector3(20,100,-20), Ogre::Quaternion::IDENTITY );
			pbody->setLinearDamping(0.8);
			pbody->attachNode( node );
			pbody->setAutoSleep(0);
			//body->setMaterialGroupID(pmat);
			pbody->setCustomForceAndTorqueCallback<Player>(&Player::move_callback, this);
			*/

			par=Vector3::ZERO;

	necknode = node->createChildSceneNode( "NeckNod" );
	necknode->setPosition(Vector3(0,1,0));
	camnode = necknode->createChildSceneNode( "CamNod" );
	camnode->attachObject(mSceneMgr->getCamera("Camera"));
	camnode->setPosition(Vector3(0,0,0));

	//ent = mSceneMgr->createEntity( "ent_podstava", "cone_p2.mesh" );
   // col_p = OgreNewt::ConvexCollisionPtr(new OgreNewt::CollisionPrimitives::ConvexHull(m_World,ent,1));	
}

Player::~Player () {
}
/*
void Player::move_callback(OgreNewt::Body* me, float timeStep, int threadIndex )
{
	me->addForce(Ogre::Vector3(0,-15,0));
	me->addForce(smer);
}
void Player::move_callback_nothing(OgreNewt::Body* me, float timeStep, int threadIndex )
{
	me->addForce(Ogre::Vector3(0,-2,0));
}
void Player::grabbed_callback(OgreNewt::Body* me, float timeStep, int threadIndex )
{
	Vector3 p;
	p=me->getOgreNode()->_getDerivedPosition();
	Vector3 p2;
	p2=necknode->_getDerivedPosition()+mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(0,0,-4);

	Vector3 s=(p2-p)*4;

	Vector3 o=me->getVelocity();
	Radian r=s.angleBetween(o);
	Real rad=r.valueDegrees();

		if(rad>45)
		{
		me->setVelocity(me->getVelocity()/2);
		s*=rad/40;
		}
	me->setForce(s);

	if(s.length()>45)
	{
	  Gbody->setMaterialGroupID(m_World->getDefaultMaterialID());
	  //Gbody->setMassMatrix(Gbody->getMass(),Gbody->getInertia()*20);
	  Gbody->setStandardForceCallback();
	  Gbody->setAngularDamping(gADT);
      Gbody->setLinearDamping(gLDT);
	  grabbed=false;
	}
}*/

void Player::pressedKey(const OIS::KeyEvent &arg){

switch (arg.key)
		{
			case OIS::KC_D: 
				setVpravo(true);
			   break;
		   case OIS::KC_A: 
			  setVlavo(true);
			   break;
		   case OIS::KC_W: 
			setVpred(true);
			   break;
		   case OIS::KC_S: 
			  setVzad(true);
			   break;

		   case OIS::KC_C: 
			   setCrouch(1);
			   break;

			 case OIS::KC_SPACE: 
				 skoc();
			break;
			  
           default:
              break;
		}
}
void Player::releasedKey(const OIS::KeyEvent &arg){ 

switch (arg.key)
		{
		   case OIS::KC_D: 
			   setVpravo(false);
			   break;
		   case OIS::KC_A: 
			   setVlavo(false);
			   break;
		   case OIS::KC_W: 
		       setVpred(false);
			   break;
		   case OIS::KC_S: 
			   setVzad(false);
			   break;

		   case OIS::KC_C: 
			   if(getCrouch()==1) setCrouch(2);
			   break;
			   
           default:
              break;
		}
}

void Player::pressedMouse(const OIS::MouseEvent &arg,OIS::MouseButtonID id){ 
/*
switch (id)
        {
		case OIS::MB_Right:	
		  if (canGrab && !grabbed){
			Gbody->setMaterialGroupID(flag_mat);
			Gbody->unFreeze();
			Gbody->setCustomForceAndTorqueCallback<Player>(&Player::grabbed_callback, this);
			//Gbody->setMassMatrix(Gbody->getMass(),Gbody->getInertia()/20);
			gADT=Gbody->getAngularDamping();
			gLDT=Gbody->getLinearDamping();
			Gbody->setAngularDamping(Vector3(1,1,1)*300);
			Gbody->setLinearDamping(300);
			grabbed=true;}

            break;
        }*/
}
void Player::releasedMouse(const OIS::MouseEvent &arg, OIS::MouseButtonID id){ 
/*
	switch (id)
        {
		case OIS::MB_Right:
			  if (grabbed){
				  Gbody->setMaterialGroupID(m_World->getDefaultMaterialID());
				  //Gbody->setMassMatrix(Gbody->getMass(),Gbody->getInertia()*20);
				  Gbody->setStandardForceCallback();
				  Gbody->setAngularDamping(gADT);
			      Gbody->setLinearDamping(gLDT);
				  grabbed=false;
			  }
            break;
        }*/
}

void Player::movedMouse(const OIS::MouseEvent &e){
rotateCamera(e.state.X.rel,e.state.Y.rel);
}


void Player::setCrouch(char b)
{ 
/*if(visi) 
{delete climbJoint; 
visi=false;
climbTimer=1;}
else 
crouch=b;*/
}

void Player::walkingSound(Ogre::Real time)
{
walkSoundTimer+=(time*pbody->getLinearVelocity().as<Ogre::Vector3>().length()/15);

if(walkSoundTimer>0.4){
NxOgre::Vec3 ppos=pbody->getGlobalPosition();
Ogre::Real rand=Ogre::Math::RangeRandom(0,5);
irrklang::ISound* music;
switch (groundID)
{

case 1:
if(rand<1.7)
music = soundEngine->play3D("../../media/Stone_Hard_Walk_01.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
else if(rand<3.4)
music = soundEngine->play3D("../../media/Stone_Hard_Walk_02.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
else
music = soundEngine->play3D("../../media/Stone_Hard_Walk_03.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
music->setMaxDistance(10);
break;
case 0:
case 2:
if(rand<1)
music = soundEngine->play3D("../../media/grass_walk_01.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
else if(rand<2)
music = soundEngine->play3D("../../media/grass_walk_02.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
else if(rand<3)
music = soundEngine->play3D("../../media/Grass_Walk_03.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
else if(rand<4)
music = soundEngine->play3D("../../media/dirt_walk_02.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
else
music = soundEngine->play3D("../../media/Dirt_Walk_03.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
music->setMaxDistance(10);
break;
case 3:
if(rand<1.25)
music = soundEngine->play3D("../../media/metalgrate1.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
else if(rand<2.5)
music = soundEngine->play3D("../../media/metalgrate2.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
else if(rand<3.75)
music = soundEngine->play3D("../../media/metalgrate3.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
else
music = soundEngine->play3D("../../media/metalgrate4.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
music->setMaxDistance(10);
break;
}

walkSoundTimer=0;}
}

void Player::skoc()
{
	if(visi)
	{
	/*delete climbJoint;
	pbody->setVelocity(pbody->getVelocity()+Vector3(0,10,0));
	visi=false;
	if(leti)
	{pbody->setCustomForceAndTorqueCallback<Player>(&Player::move_callback, this); leti=false;}
	climbTimer=0.5;*/
	}
	else
	//if(nazemi)
	{
	//Vector3 ppos=pbody->getPosition();
	//irrklang::ISound* music;
	//music = soundEngine->play3D("../../media/dych.wav",irrklang::vec3df(ppos.x,ppos.y,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
//	music->setMaxDistance(10);
	jumpPitchsize=0;
	jumpPitch=1;
	}
}
void Player::manageFall()
{

	zrychlenie=0.5;
	veee=abs(lastSpeed)*2;
	fallPitchsize=0;
	//necknode->pitch(Degree(-veee/3), Node::TS_LOCAL);
	fallPitch=1;
	NxOgre::Vec3 ppos=pbody->getGlobalPosition();
	irrklang::ISound* music;
	
	Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(Ogre::StringConverter::toString(veee),Ogre::LML_NORMAL);

	if(veee>50)
	{
	veee=80;
	*postPr1=4;
	slowingDown=0;
	irrklang::ISound* music2;
	music2 = soundEngine->play3D("../../media/pad.wav",irrklang::vec3df(ppos.x,ppos.y,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
	music2->setMaxDistance(10);
	}
	
	switch (groundID)
	{
	case 1:
	music = soundEngine->play3D("../../media/Stone_Hard_Walk_02.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
	music->setMaxDistance(10);
	break;
	case 0:
	case 2:
	music = soundEngine->play3D("../../media/grass_run_02.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
	music->setMaxDistance(10);
	break;
	case 3:
	music = soundEngine->play3D("../../media/metalgrate1.wav",irrklang::vec3df(ppos.x,ppos.y-2,ppos.z), false, false, true, irrklang::ESM_AUTO_DETECT, false);
	music->setMaxDistance(10);
	break;
	
	}
	
}

void Player::rotateCamera(Real hybX,Real hybY)
{
		hybY*=-0.1;
		camAngle+=(hybY);
		if (camAngle>-80 && camAngle<80)
			necknode->pitch(Degree(hybY), Node::TS_LOCAL);
		else { camAngle=camAngle-hybY; 
			if (camAngle<0) {necknode->pitch(Degree(-80-camAngle), Node::TS_LOCAL); camAngle=-80.0;} 
			if (camAngle>0) {necknode->pitch(Degree(80-camAngle), Node::TS_LOCAL); camAngle=80.0;} 
			  } 

		if(!visi)
		necknode->yaw(Degree(-0.1 * hybX), Node::TS_WORLD);
		else
		{
			Vector3 pohlad=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(0,0,-1); 
			pohlad.y=0; 
			pohlad.normalise();
			Real angle=climbn.angleBetween(pohlad).valueDegrees();
			

			Real climbCam= climbn.getRotationTo(pohlad).getYaw().valueDegrees();

			if((climbCam>0 && hybX<0) || (climbCam<0 && hybX>0))
			{
			necknode->yaw(Degree(-0.1 * hybX), Node::TS_WORLD);
			}
			else
			{
			Real spomal;
			angle-=90;
			if(angle<0) spomal=0;
			else spomal=angle/90;
			necknode->yaw(Degree(-0.1 * hybX* spomal), Node::TS_WORLD);
			}

		}

}

void Player::updateHead(Real time)
{

	//move speed+sound
	if(!stoji && nazemi) 
	{
	if (movespeed<17) movespeed+=time*10;
	if (movespeed>17) movespeed=17;
	walkingSound(time);}
	else movespeed=7;

	//dont climb
	if (climbTimer>0) {climbTimer-=time;}

	
	if(crouch>0)
	{
		//crouching
		if(crouch==1)
		{
		if(crouch_am>-1.5) crouch_am-=time*8;
		if(crouch_am<-1.5) crouch_am=-1.5;
		necknode->setPosition(0,1+crouch_am,0);
		}
		if(crouch==2)
		{
		crouch_am+=time*8;
		if(crouch_am>0) {crouch_am=0; crouch=0;}
		necknode->setPosition(0,1+crouch_am,0);
		}

		if(fallPitch==1)
		{fallPitchsize+=time; necknode->pitch(Degree(veee*time*-3), Node::TS_LOCAL); if(fallPitchsize>0.1) fallPitch=2;}
		else if(fallPitch==2)
		{fallPitchsize-=time; necknode->pitch(Degree(veee*time*1.5), Node::TS_LOCAL); if(fallPitchsize<=-0.1) fallPitch=0;}

		if(jumpPitch==1)
		{jumpPitchsize+=time; necknode->pitch(Degree(time*-10), Node::TS_LOCAL); if(jumpPitchsize>0.1) jumpPitch=2;}
		else if(jumpPitch==2)
		{jumpPitchsize-=time; necknode->pitch(Degree(time*5), Node::TS_LOCAL); if(jumpPitchsize<=-0.1) jumpPitch=0;}
	}

	else
	//not crouching
	{
	Ogre::Real pitchTime=time;

	if(fallPitch==1)
	{
	if((fallPitchsize+pitchTime)>0.1) pitchTime=0.1-fallPitchsize; 
	fallPitchsize+=pitchTime; necknode->pitch(Degree(veee*pitchTime*-3), Node::TS_LOCAL); if(fallPitchsize>=0.1) fallPitch=2;
	}
	else if(fallPitch==2)
	{
	if((fallPitchsize-pitchTime)<-0.1) pitchTime=0.1+fallPitchsize; 
	fallPitchsize-=pitchTime; necknode->pitch(Degree(veee*pitchTime*1.5), Node::TS_LOCAL); if(fallPitchsize<=-0.1) fallPitch=0;
	}

	if(jumpPitch==1)
	{
	jumpPitchsize+=time; necknode->pitch(Degree(time*-10), Node::TS_LOCAL); 
		if(jumpPitchsize>0.1) 
		{jumpPitch=2; NxOgre::Vec3 vel=pbody->getLinearVelocity().as<Ogre::Vector3>();	pbody->setLinearVelocity(vel+NxOgre::Vec3(0,18,0)); }
	}
	else if(jumpPitch==2)
	{
	jumpPitchsize-=time; necknode->pitch(Degree(time*5), Node::TS_LOCAL); if(jumpPitchsize<=-0.1) jumpPitch=0;
	}
	

	if(!stoji && nazemi && (pbody->getLinearVelocity().magnitude()>2))
	{
	//walking camera
	cameraWalkFinisher=1;
	cam_walking+=time*15*pbody->getLinearVelocity().as<Ogre::Vector3>().length()/15;
	camnode->setPosition(Ogre::Math::Sin(cam_walking/2+Math::PI,false)/12,Ogre::Math::Sin(cam_walking,false)/30,0);
	camnode->roll(Degree(Ogre::Math::Sin(cam_walking/2+Math::PI/2))/30);

	/*
	//adjust speedwhen turning, deprecated
	if(vpred && !vlavo && !vpravo){
		Vector3 s=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(0,0,-1); 
		s.y=0; 
		s.normalise();
		Vector3 o=pbody->getLinearVelocity().as<Ogre::Vector3>();
		o.y=0;
		o.normalise();
		Quaternion ss=s.getRotationTo(o);
		Real r=ss.zAxis().x;
		Vector3 so(r*15,0,0);
		smer+=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*so;}
	*/
	}

	else if (camnode->getPosition().x>0.01 || camnode->getPosition().x<-0.01)
	{
		if(cameraWalkFinisher==1)
		{
		if (camnode->getPosition().x>0.01) cameraWalkFinisher=2;
		if (camnode->getPosition().x<-0.01) cameraWalkFinisher=3;
		}

		cam_walking+=time*15;
		camnode->setPosition(Ogre::Math::Sin(cam_walking/2+Math::PI,false)/12,Ogre::Math::Sin(cam_walking,false)/30,0);
		camnode->roll(Degree(Ogre::Math::Sin(cam_walking/2+Math::PI/2))/30);

		if (camnode->getPosition().x<=0.01 && cameraWalkFinisher==2) cameraWalkFinisher=0;
		if (camnode->getPosition().x>=-0.01 && cameraWalkFinisher==3) cameraWalkFinisher=0;	
	}
	else
	if(!cameraWalkFinisher)
	{
		camnode->setPosition(0,0,0);
		necknode->setPosition(0,1,0);
		camnode->resetOrientation();
	}
	}
}

void Player::update(Real time)
{

		tslf=time;

		updateStats();



		/*if(iii){
		Vector3 bodPohladu(0,0,100);
	Vector3 sm=bodPohladu-camnode->getPosition();
    sm.normalise();
	iii=false;

	//mSceneMgr->getCamera("Camera")->lookAt(Vector3(0,0,100));
	Vector3 d= mSceneMgr->getCamera("Camera")->getDerivedDirection();
	d.normalise();

	camnode->pitch(d.getRotationTo(sm).getPitch(), Node::TS_LOCAL);
	camnode->yaw(d.getRotationTo(sm).getYaw(), Node::TS_WORLD);

	//par.x=d.getRotationTo(sm).getPitch().valueDegrees();
	par.y=d.getRotationTo(sm).getRoll().valueDegrees();
	par.z=d.getRotationTo(sm).getYaw().valueDegrees();

	Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(Ogre::StringConverter::toString(par),Ogre::LML_NORMAL);
		}*/


		if(slowingDown<1)
		{slowingDown+=(tslf/2);
		if(slowingDown>1) slowingDown=1;}


		smer=Vector3::ZERO;

		if (!vpravo && !vpred && !vzad && !vlavo) {pbody->getShape(0)->setMaterial(mScene->getMaterial(2)->getIdentifier()); stoji=true; zrychlenie=0.5; walkSoundTimer=0.37; }else
		{
		if(zrychlenie<1){zrychlenie+=tslf/2; if(zrychlenie>1) zrychlenie=1;}
		pbody->getShape(0)->setMaterial(mScene->getMaterial(1)->getIdentifier()); stoji=false; 
		if (!vlavo && !vpravo) {
			if (vpred){smer=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(0,0,-1); smer.y=0; smer.normalise();}
		if (vzad) {smer=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(0,0,1); smer.y=0; smer.normalise();}} else  
		{
			if (vpravo){
				if (!vpred && !vzad) {smer=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(1,0,0); smer.y=0; smer.normalise();} else
				if (vpred) {smer=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(1,0,-1); smer.y=0; smer.normalise();} else
				if (vzad) {smer=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(1,0,1); smer.y=0; smer.normalise();} 
				}

			if (vlavo){
				if (!vpred && !vzad) {smer=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(-1,0,0); smer.y=0; smer.normalise();} else
				if (vpred) {smer=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(-1,0,-1); smer.y=0; smer.normalise();} else
				if (vzad) {smer=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(-1,0,1); smer.y=0; smer.normalise();} 
			}
		}  
		} 
		
		if(nazemi)
		{
			pbody->setLinearDamping(5);
			Ogre::Vector3 s=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Ogre::Vector3(0,0,-1); 
			s.y=0; 
			s.normalise();
			NxOgre::Vec3 oo=pbody->getLinearVelocity();
			Ogre::Vector3 o(oo.x,oo.y,oo.z);
			o.y=0;
			Ogre::Radian r=s.angleBetween(o);

			Ogre::Real rad=r.valueDegrees();
			if(rad>45 && vpred && !vzad && !vpravo && !vlavo)
				smer*=rad/25;
			smer*=zrychlenie*3;
		}
		else
		{
		pbody->setLinearDamping(0);
		smer*=0.15;
		}

		pbody->addForce(smer*35*tslf,NxOgre::Enums::ForceMode_SmoothVelocityChange);
		/*if (nazemi) {
				Vector3 s=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(0,0,-1); 
		s.y=0; 
		s.normalise();
		Vector3 o=pbody->getLinearVelocity().as<Ogre::Vector3>();
		o.y=0;
		Radian r=s.angleBetween(o);
		Vector3 so;
		so.y=0;
		so.x=(-Ogre::Math::Sin(r)*-1);
		so.z=(Ogre::Math::Cos(r)*-1);
		so.normalise();

		if(gNormal.y>0.8)
		smer+=(1-gNormal.y)*smer*2;
		//smer+=(1-gNormal.y)*2; 
		Real rad=r.valueDegrees();
		if(rad>45 && vpred && !vzad && !vpravo && !vlavo)
		smer*=movespeed*rad/30;

		else smer*=movespeed*slowingDown;
		
		if(crouch>0) smer/=3;} 

		else smer*=3;*/



		updateHead(time);

}



void Player::updateStats()
{

	/*Vector3 p;
	p=pbody->getGlobalPosition().as<Ogre::Vector3>();

	NxOgre::Ray ray;
	ray.mOrigin=pbody->getGlobalPosition();
	ray.mDirection=NxOgre::Vec3(0,-1,0);
	NxOgre::RaycastHit rh= mScene->raycastClosestShape(ray,NxOgre::Enums::ShapesType_Static);
	
	if(rh.mDistance<0.7)
	{
	if(!nazemi) manageFall();
	nazemi=true;
	gNormal=rh.mWorldNormal.as<Ogre::Vector3>();}
	else
	{
	nazemi=false;
	lastSpeed=pbody->getLinearVelocity().y;
	}*/
	NxOgre::buffer<NxOgre::Shape *> shapes;
	//shapes.push_back(p->getBody()->getShape(0));
	int count=mScene->overlapSphereShape(NxOgre::SimpleSphere(0.5f,pbody->getGlobalPosition()-NxOgre::Vec3(0,1.5,0)),NxOgre::Enums::ShapesType_Static,2,shapes);
	if(count>1)
	{
	if(!nazemi) manageFall();
	nazemi=true;
	//gNormal=rh.mWorldNormal.as<Ogre::Vector3>();
	}
	else
	{
	nazemi=false;
	lastSpeed=pbody->getLinearVelocity().y;
	}


	groundID=0;
	/*
	OgreNewt::BasicRaycast* ray = new OgreNewt::BasicRaycast( m_World,(p-Vector3(0,1.7,0)),(p-Vector3(0,2.3,0))  ,true);
		OgreNewt::BasicRaycast::BasicRaycastInfo info = ray->getInfoAt(0);

			if (info.mBody)
			{
				Ogre::Any any = info.mBody->getUserData();
				if(!any.isEmpty())
			    groundID=any_cast<int>(any);
				else
				groundID=0;

				if(!nazemi) manageFall();
				nazemi=true;
				pbody->setLinearDamping(2);
				gNormal=info.mNormal;
			} else { 
			
			OgreNewt::BasicConvexcast* rayc = new OgreNewt::BasicConvexcast( m_World,col_p,(p-Vector3(0,1.5,0)),Ogre::Quaternion::IDENTITY,(p-Vector3(0,2.0,0)),1,1);
		OgreNewt::BasicConvexcast::ConvexcastContactInfo infoc = rayc->getInfoAt(0);
		if (infoc.mBody)
			{	
				Ogre::Any any = infoc.mBody->getUserData();
				if(!any.isEmpty())
			    groundID=any_cast<int>(any);
				else
				groundID=0;
	
				nazemi=true;
				gNormal.y=1;
				pbody->setLinearDamping(2);

			} 
			 else 
			{
				groundID=-1;
				lastSpeed=pbody->getVelocity().y;
				nazemi=false; pbody->setLinearDamping(0.0); gNormal.y=1;} 

			delete rayc;

			}

		delete ray;
		
		p=necknode->_getDerivedPosition();
		Vector3 predsebou=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(0,0,-1);
		predsebou.y=0; 
		predsebou.normalise();
		predsebou=predsebou*2+p;


		
		ray = new OgreNewt::BasicRaycast( m_World,(p+Vector3(0,0.5,0)),(predsebou+Vector3(0,0.5,0))  ,true);
		info = ray->getInfoAt(0);
			if (info.mBody)
			{
				moze_liezt=true;
				if(!visi && climbTimer<=0)
				{
					if(info.mBody->getType()==4)
					{
					Ogre::Any any = info.mBody->getOgreNode()->getUserAny();
					any_cast<Baloon*>(any)->setTracker(track);
					climbJoint = new OgreNewt::BallAndSocket(info.mBody, pbody, p ); 
					visi=true; 
					leti=true;
					pbody->setCustomForceAndTorqueCallback<Player>(&Player::move_callback_nothing, this);
					}

					if(info.mBody->getType()==2)
					{
					climbJoint = new OgreNewt::BallAndSocket(info.mBody, pbody, p ); 
					visi=true; 
					climbn=info.mNormal;

					Vector3 pohlad=mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(0,0,-1); 
					pohlad.y=0; 
					pohlad.normalise();
					climbCam=climbn.angleBetween(pohlad).valueDegrees();
					
					}
				}
			}
			else {moze_liezt=false;}

		delete ray;

		if(!grabbed)
		{
		ray = new OgreNewt::BasicRaycast( m_World,p,p+mSceneMgr->getCamera("Camera")->getDerivedOrientation()*Vector3(0,0,-5)  ,true);
		info = ray->getInfoAt(0);
			if (info.mBody)
			{
				if (info.mBody->getType()==3){									
				Gbody=info.mBody;
				canGrab=true;}
			}
		delete ray;
		}
*/
		
		

}



void Player::trackCamera(Ogre::Vector3 direction, int maxGoodDegree, int power)
{

}